<template>
  <div class="dashboard-container">
    <!-- 顶部统计卡片 -->
    <el-row :gutter="20" class="mb-20">
      <el-col :xs="24" :sm="12" :md="6" v-for="item in statsData" :key="item.title">
        <el-card shadow="hover" class="stat-card">
          <div class="flex items-center">
            <el-icon :size="40" :color="item.color" class="mr-10">
              <component :is="item.icon" />
            </el-icon>
            <div>
              <div class="text-gray-500 text-sm">{{ item.title }}</div>
              <div class="text-2xl font-bold">{{ item.value }}</div>
              <div class="text-xs mt-2" :style="{ color: item.color }">
                <el-icon :size="12"><arrow-up /></el-icon>
                {{ item.rate }}%
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 主要图表区域 -->
    <el-row :gutter="20" class="mb-20">
      <el-col :xs="24" :md="16">
        <el-card shadow="hover">
          <div ref="lineChartRef" style="height: 400px"></div>
        </el-card>
      </el-col>
      <el-col :xs="24" :md="8">
        <el-card shadow="hover">
          <div ref="pieChartRef" style="height: 400px"></div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 最新动态表格 -->
    <el-card shadow="hover">
      <template #header>
        <div class="flex justify-between items-center">
          <span>最新订单</span>
          <el-button type="primary" link>查看更多</el-button>
        </div>
      </template>
      <el-table :data="orderData" style="width: 100%">
        <el-table-column prop="id" label="订单号" width="180" />
        <el-table-column prop="customer" label="客户" />
        <el-table-column prop="amount" label="金额" align="right">
          <template #default="{ row }">¥{{ row.amount.toLocaleString() }}</template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="120">
          <template #default="{ row }">
            <el-tag :type="statusTagType(row.status)">{{ row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="日期" width="180" />
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'
import * as echarts from 'echarts'
import {
  User,
  ShoppingCart,
  Money,
  ArrowUp,
  List
} from '@element-plus/icons-vue'

// 统计卡片数据
const statsData = ref([
  { title: '总用户数', value: '12,345', rate: 12.5, icon: User, color: '#409EFF' },
  { title: '订单总量', value: '2,345', rate: 8.2, icon: ShoppingCart, color: '#67C23A' },
  { title: '销售额', value: '¥345,678', rate: 18.4, icon: Money, color: '#E6A23C' },
  { title: '商品数量', value: '456', rate: 5.6, icon: List, color: '#F56C6C' }
])

// 订单数据
const orderData = ref([
  { id: '20230728001', customer: '张三', amount: 1280, status: '已完成', date: '2023-07-28' },
  { id: '20230728002', customer: '李四', amount: 2560, status: '待发货', date: '2023-07-28' },
  { id: '20230728003', customer: '王五', amount: 3580, status: '已付款', date: '2023-07-28' },
])

// 图表相关
const lineChartRef = ref<HTMLElement>()
const pieChartRef = ref<HTMLElement>()
let lineChart: echarts.ECharts | null = null
let pieChart: echarts.ECharts | null = null

// 初始化折线图
const initLineChart = () => {
  if (!lineChartRef.value) return
  
  lineChart = echarts.init(lineChartRef.value)
  lineChart.setOption({
    title: { text: '销售趋势', left: 'center' },
    xAxis: {
      type: 'category',
      data: ['1月', '2月', '3月', '4月', '5月', '6月']
    },
    yAxis: { type: 'value' },
    series: [{
      data: [820, 932, 901, 934, 1290, 1330],
      type: 'line',
      smooth: true
    }]
  })
}

// 初始化饼图
const initPieChart = () => {
  if (!pieChartRef.value) return
  
  pieChart = echarts.init(pieChartRef.value)
  pieChart.setOption({
    title: { text: '销售占比', left: 'center' },
    tooltip: { trigger: 'item' },
    series: [{
      type: 'pie',
      radius: '50%',
      data: [
        { value: 1048, name: '电子产品' },
        { value: 735, name: '服装服饰' },
        { value: 580, name: '家居用品' }
      ],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }]
  })
}

// 状态标签类型映射
const statusTagType = (status: string) => {
  const map: Record<string, string> = {
    '已完成': 'success',
    '待发货': 'warning',
    '已付款': ''
  }
  return map[status] || 'info'
}

// 处理窗口大小变化
const handleResize = () => {
  lineChart?.resize()
  pieChart?.resize()
}

onMounted(() => {
  initLineChart()
  initPieChart()
  window.addEventListener('resize', handleResize)
})

onBeforeUnmount(() => {
  window.removeEventListener('resize', handleResize)
  lineChart?.dispose()
  pieChart?.dispose()
})
</script>

<style lang="scss" scoped>
.dashboard-container {
  padding: 20px;
  
  .mb-20 {
    margin-bottom: 20px;
  }

  .stat-card {
    margin-bottom: 20px;
    transition: transform 0.3s;
    
    &:hover {
      transform: translateY(-5px);
    }

    :deep(.el-card__body) {
      padding: 20px;
    }
  }

  .el-icon {
    padding: 8px;
    border-radius: 8px;
    background-color: var(--el-color-primary-light-9);
  }
}
</style>